using System;
using System.Collections;
using System.Collections.Generic;
using GameFramework.buff;
using GameFramework.skill;
using GameFramework.skill.effect;
using GameFramework.skill.effect.impl;
using UnityEngine;

public class BulletCtl : MonoBehaviour
{
    public GameObject target;
    private BulletConfig _config;
    private Vector3 targetPos;
    private Coroutine coroutine;
    public Skill _skill;

    private List<ISkillEffect> _effects = new List<ISkillEffect>();
    // Start is called before the first frame update

    public BulletConfig config
    {
        get => _config;
        set
        {
            _config = value;
            initSkillEffects();
        }
    }

    private void initSkillEffects()
    {
        _effects.Clear();
        foreach (int effectId in _config.effectIds)
        {
            EffectConfig effectConfig = EffectConfigManager.Instance.getConfig(effectId);
            //TODO 这些脚本应该映射管理起来
            if (effectConfig.type == 1)
            {
                DamigeHpSkillEffect effect = new DamigeHpSkillEffect();
                effect.effectConfig = effectConfig;
                _effects.Add(effect);
            }
            if (effectConfig.type == 3)
            {
                AddBuffSkillEffect effect = new AddBuffSkillEffect();
                effect.effectConfig = effectConfig;
                _effects.Add(effect);
            }
        }
    }

    public void go()
    {
        coroutine = StartCoroutine(move());
    }

    private void OnDestroy()
    {
        if (coroutine != null)
        {
            StopCoroutine(coroutine);
        }
    }

    private IEnumerator move()
    {

        if (target == null && targetPos==null)
        {
            Debug.Log("mm1");
            yield break;
        }

        
        while (true)
        {
            if (target != null)
            {
                targetPos = target.transform.position;
            }
            //TODO 这里有隐含问题，如果子弹速度过大，移动一帧后与目标距离仍很大，会造成子弹来回飞的情况
            if (Vector3.Distance(transform.position, targetPos) <= 0.2)
            {
                //到达目标点
                if (target == null)
                {
                    Destroy(gameObject);  
                    yield break;
                }
                //TODO 显示击中特效
                
                //执行击中效果
                foreach (ISkillEffect effect in _effects)
                {
                    try
                    {
                        Debug.Log("子弹到达目的地，执行子弹击中效果！");
                        effect.excute(_skill);
                        //TODO 根据id创建效果并执行
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e);
                    }

                }
                Debug.Log("子弹到达目的地，自我销毁");
                Destroy(gameObject);
                yield break;
            }

            if (config.type == 1)//追踪子弹
            {
            
                trackTarget();
                yield return null;
            }
            else
            {
                //线性子弹
                directMove();
                yield return null;
            }
        }

    }

    // Update is called once per frame
    void Update()
    {

    }

    private void directMove()
    {
        
    }

    /// <summary>
    /// 追踪目标
    /// </summary>
    private void trackTarget()
    {
        if (config.bezier) //贝塞尔运动
        {
            
        }
        else//
        {
            // Vector3 direction = (target.transform.position - transform.position).normalized;
            // transform.Translate(direction*Time.deltaTime*config.speed,Space.World);
            transform.position = Vector3.MoveTowards(transform.position, targetPos, Time.deltaTime * config.speed);
        }
    }
}
